package com.jvxb.manage.livable.controller;

import com.baomidou.mybatisplus.core.conditions.Condition;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jvxb.common.utils.CommonUtil;
import com.jvxb.common.web.RespMsg;
import com.jvxb.manage.livable.entity.WorkOrder;
import com.jvxb.manage.livable.service.WorkOrderService;
import com.jvxb.manage.livable.vo.WorkOrderOperateVo;
import com.jvxb.modules.configuration.security.SecurityUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jvxb
 * @since 2022-01-07
 */
@Log4j2
@RestController
@RequestMapping("/workOrder/v1")
public class WorkOrderController {

    @Autowired
    private WorkOrderService workOrderService;

    @PostMapping("/add")
    public RespMsg add(@RequestBody WorkOrder workOrder) {
        workOrderService.save(workOrder);
        return RespMsg.ok();
    }

    @PostMapping("/delete")
    public RespMsg delete(@RequestBody WorkOrder workOrder) {
        workOrderService.removeById(workOrder);
        return RespMsg.ok();
    }

    @PostMapping("/update")
    public RespMsg update(@RequestBody WorkOrder workOrder) {
        workOrderService.saveOrUpdate(workOrder);
        return RespMsg.ok();
    }

    @GetMapping("/getById")
    public RespMsg getById(Long id) {
        WorkOrder workOrder = workOrderService.getById(id);
        return RespMsg.ok(workOrder);
    }

    @GetMapping("/list")
    public RespMsg list() {
        List<WorkOrder> list = workOrderService.list(null);
        return RespMsg.ok(list);
    }

    @GetMapping("listPage")
    public RespMsg listPage(String searchInput,
                            Integer searchTodo,
                            String searchMyExecute,
                            @RequestParam(defaultValue = "10") Integer size,
                            @RequestParam(defaultValue = "1") Integer current) {
        IPage<WorkOrder> workOrderIPage = new Page<>(current, size);
        QueryWrapper<WorkOrder> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc(WorkOrder.ID);
        if (CommonUtil.notNullOrEmpty(searchInput)) {
            wrapper.like(WorkOrder.CREATE_DESC, searchInput).or().like(WorkOrder.CREATOR, searchInput);
        }
        if (CommonUtil.notNullOrEmpty(searchTodo) && searchTodo == 1) {
            wrapper.like(WorkOrder.ASSIGNEE, String.format("\"userCode\":\"%s\"", SecurityUtil.currentUserId()));
        }
        if (CommonUtil.notNullOrEmpty(searchMyExecute)) {
            wrapper.eq(WorkOrder.EXECUTE_ID, searchMyExecute);
        }
        workOrderService.page(workOrderIPage, wrapper);
        return RespMsg.ok(workOrderIPage);
    }

    @GetMapping("/countTodoWorkOrder")
    public RespMsg countTodoWorkOrder() {
        int workOrderCount = workOrderService.count(Condition.<WorkOrder>create().like(WorkOrder.ASSIGNEE, String.format("\"userCode\":\"%s\"", SecurityUtil.currentUserId())));
        return RespMsg.ok(workOrderCount);
    }

    @GetMapping("/getTodoMsg")
    public RespMsg getTodoMsg() {
        return RespMsg.ok(workOrderService.getInformMsgMap(SecurityUtil.currentUserId().toString()));
    }

    /**
     * 工单处理
     *
     * @param operateVo
     * @return
     */
    @PostMapping("/operate")
    public RespMsg operate(@RequestBody WorkOrderOperateVo operateVo) {
        try {
            workOrderService.operate(operateVo);
            return RespMsg.ok();
        } catch (Exception e) {
            log.error("处理工单[{}]异常，请联系平台值班查看", operateVo.getId(), e);
            return RespMsg.error("处理工单异常，请联系平台值班查看。");
        }
    }

    /**
     * 工单执行
     *
     * @param operateVo
     * @return
     */
    @PostMapping("/executeWorkOrder")
    public RespMsg executeWorkOrder(@RequestBody WorkOrderOperateVo operateVo) {
        workOrderService.executeWorkOrder(operateVo);
        return RespMsg.ok();
    }

}
